# -*- coding: utf-8 -*-
from random import shuffle
import time


class DictEstados(dict):

    def __init__(self):
        dict.__init__()

class AjedrezAfricano():
    
    def __init__(self):
        self.board = {}
        self.board[0] = [ 3 for i in range(6) ] # casillas jugador 1
        self.board[1] = [ 3 for i in range(6) ] # casillas jugador 2
        self.board[2] = [ 0 ] # ganadas jugador 1
        self.board[3] = [ 0 ] # ganadas jugador 2
        
        self.turn = 0
        self.moves = 0
        
    
    def show_board(self):
        out = ""
        #### raya
        for i in range(6*4+4+1):
            out = out + "-"    
        out = out + "\n"
        
        #### jugador 1
        out = out + "| " + str(self.board[2][0]) 
        if self.board[2][0]<10:
            out += " " 
        for i in range(6*4+1):
            if i%4==0: 
                out = out + "|"
            elif i%4==1: 
                out = out + " "
            elif i%4==2: 
                out = out + str(self.board[0][i/4])
            else: 
                if(self.board[0][i/4])<10:
                    out = out + " "
        out = out + "\n"
        
        #### raya
        for i in range(6*4+4+1+4):
            out = out + "-"
        out = out + "\n"

        #jugador 2        
        out = out + "    "
        for i in range(6*4+1):
            if i%4==0: 
                out = out + "|"
            elif i%4==1: 
                out = out + " "
            elif i%4==2: 
                out = out + str(self.board[1][5-i/4])
            else: 
                if(self.board[1][i/4])<10:
                    out = out + " "
        out = out + " " + str(self.board[3][0]) 
        if self.board[3][0]<10:
            out += " " 
        out += "|" 
        out = out + "\n"

        #### raya
        out = out + "    "
        for i in range(6*4+4+1):
            out = out + "-"
        out = out + "\n"




        
        print out
    
    
    def eoe(self):
        return sum(self.board[self.turn]) == 0
    
    def is_moveable(self, player, pos):
        return self.board[player][pos] > 0
    
    def make_move(self, player, pos):
        current_player = player
        if self.board[player][pos] == 0:
            print "Error. No hay fichas ahí"
        else:
            self.moves += 1
            free_rocks = self.board[player][pos]
            self.board[player][pos] = 0
            
            pos -=  1 
            if pos==-1:
                if free_rocks==1 and current_player==player:
                    self.board[current_player+2][0] = self.board[current_player+2][0] + 1
                    free_rocks = 0
                else:
                    pos = 5
                    current_player = (current_player+1)%2

            while (free_rocks>0):
                self.board[current_player][pos] += 1
                free_rocks -= 1
                pos -=  1 

                if pos==-1:
                    if free_rocks==1 and current_player==player:
                        self.board[current_player+2][0] = self.board[current_player+2][0] + 1
                        free_rocks = 0
                    else:
                        pos = 5
                        current_player = (current_player+1)%2

    def change_turn(self):
        self.turn = (self.turn+1)%2
    
    def make_random_move(self):
        a = [ i for i in range(6) ]
        shuffle(a)
        
        for i in a:
            if self.is_moveable(self.turn, i):
                self.make_move(self.turn, i)
                self.change_turn()
                break
    
    def winner(self):
        p1 = self.board[2][0] - sum(self.board[0])
        p2 = self.board[3][0] - sum(self.board[1])
        
        if p1 == p2:
            return 0
        if p1 > p2:
            return 1
        if p1 < p2:
            return -1

    def print_winner(self):
        res = self.winner()
        if res==1:
            return "Ganó el jugador 1"
        if res==0:
            return "Hubo empate"
        if res==-1:
            return "Ganó el jugador 2"

    def get_state_number(self):
        res = 0
        multiplier = 0
        for i in range(6):
            res += self.board[0][i] * 37**multiplier
            multiplier+=1
        for i in range(6):
            res += self.board[1][i] * 37**multiplier
            multiplier+=1
        for i in range(2):
            res += self.board[2+i][0] * 37**multiplier
            multiplier+=1
            
        print multiplier
        return res

    def set_board_from_number(self, n):
        b = [
              [ -1 for i in range(6) ], # casillas jugador 1b[0] = [ -1 
              [ -1 for i in range(6) ], # casillas jugador 2
              [ -1 ], # ganadas jugador 1
              [ -1 ], # ganadas jugador 2
            ]
        
        multiplier = 13
        
        b[3] = [int(n/(37**multiplier))]
        n = n % (37**multiplier)
        multiplier-=1
        
        b[2] = [int(n/(37**multiplier))]
        n = n % (37**multiplier)
        multiplier-=1
        
        for i in range(6):
            b[1][5-i] = int(n/(37**multiplier))
            n = n % (37**multiplier)
            multiplier-=1
        
        for i in range(6):
            b[0][5-i] = int(n/(37**multiplier))
            n = n % (37**multiplier)
            multiplier-=1
        
        print "last multiplier %d" %multiplier
        return b
        
def main():
    game = AjedrezAfricano()
    #~ game.make_move(0,0)
    
    while not(game.eoe()):
        gn = game.get_state_number()
        print gn
        game.show_board()
        
        #~ print "Real-board"
        #~ print game.board
        print "Rebuild-board"
        print game.set_board_from_number(gn)
        
        game.make_random_move()
        #~ time.sleep(6)
        
    print "Terminó en %d movidas" % game.moves
    print game.print_winner()
    
if __name__ == '__main__':
    main()
